'use strict';

const gulp = require('gulp');

const nodemon = require('gulp-nodemon');

const eslint = require('gulp-eslint');

const browserSync = require('browser-sync').create();// 静态服务器

gulp.task('nodemon', function (cb) {

    let started = false;

    return nodemon({
        script: 'app.js',
        ext: 'js json',
        //env: { 'NODE_ENV': 'development' },
        ignore: [
            'var/',
            'node_modules/',
            'app/public/'
        ],
        watch: ['app/*', '!app/public/']
        // ,stdout:   false
        //  ,readable: false
    }).on('start', function () {
        if (!started) {
            cb();
            started = true;
        }
    })

});

// gulp.task('lint', ()=> {
//     return gulp.src(['./app/**/*.js','!node_modules/**'])
//         .pipe(eslint())
//         // eslint.format() outputs the lint results to the console.
//         // Alternatively use eslint.formatEach() (see Docs).
//         .pipe(eslint.format())
//         // To have the process exit with an error code (1) on
//         // lint error, return the stream and pipe to failAfterError last.
//         .pipe(eslint.failAfterError());
// });
gulp.task('lint', function () {
    return gulp.src(['app/**/*.js'])
        .pipe(eslint({configFile: './eslint.json'}))
        .pipe(eslint.format())
        .pipe(eslint.failAfterError());
});



gulp.task('browser-sync', function () {

    browserSync.init({
        port: 9000,
        proxy: 'http://127.0.0.1:3000'
    });
});

gulp.task('watch', ['nodemon'], function () {
    gulp.watch([
        'app/**/*.js'
    ]);

});
gulp.task('default', ['browser-sync', 'watch']);
